package view;

import controller.ClienteDao;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.ClienteBean;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author fp0520111064
 */
public class FrmCadastroCliente extends javax.swing.JFrame {

    public List<ClienteBean> clientes = new ArrayList<ClienteBean>();
    public int intPosition = 0;
    public boolean booInclusao = false;
    public boolean booAlteracao = false;
    private static FrmCadastroCliente _instance = null;

    public static synchronized FrmCadastroCliente getInstance() {
        if (_instance == null) {
            _instance = new FrmCadastroCliente();
        }
        return _instance;
    }

    /**
     * Creates new form FrmCadastroCliente
     */
    private FrmCadastroCliente() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        pnlBotoes = new javax.swing.JPanel();
        lblBotaoAdicionar = new javax.swing.JLabel();
        lblBotaoAlterar = new javax.swing.JLabel();
        lblBotaoDeletar = new javax.swing.JLabel();
        lblBotaoSalvar = new javax.swing.JLabel();
        lblBotaoCancelar = new javax.swing.JLabel();
        pnlControlesCli = new javax.swing.JPanel();
        btnVoltarInicioCli = new javax.swing.JButton();
        btnVoltarCli = new javax.swing.JButton();
        lblContador1Cli = new javax.swing.JLabel();
        lblContador2 = new javax.swing.JLabel();
        lblContador3 = new javax.swing.JLabel();
        btnSeguirCli = new javax.swing.JButton();
        btnUltimoCli = new javax.swing.JButton();
        lblCodCli = new javax.swing.JLabel();
        lblNomeCli = new javax.swing.JLabel();
        lblDataCadastro = new javax.swing.JLabel();
        lblTEL = new javax.swing.JLabel();
        lblCEL = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        txtCod = new javax.swing.JTextField();
        txtData = new javax.swing.JFormattedTextField();
        txtTelefone = new javax.swing.JFormattedTextField();
        txtCelular = new javax.swing.JFormattedTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Cadastrar Clientes");
        setBackground(new java.awt.Color(255, 255, 204));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        lblBotaoAdicionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/img/add-2-icon (2)_1.png"))); // NOI18N
        pnlBotoes.add(lblBotaoAdicionar);
        pnlBotoes.add(lblBotaoAlterar);

        lblBotaoDeletar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/img/delete-icon.png"))); // NOI18N
        pnlBotoes.add(lblBotaoDeletar);

        lblBotaoSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/img/basic-data-icon.png"))); // NOI18N
        pnlBotoes.add(lblBotaoSalvar);
        pnlBotoes.add(lblBotaoCancelar);

        pnlControlesCli.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        pnlControlesCli.setOpaque(false);

        btnVoltarInicioCli.setText("|<");
        btnVoltarInicioCli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVoltarInicioCliActionPerformed(evt);
            }
        });
        pnlControlesCli.add(btnVoltarInicioCli);

        btnVoltarCli.setText("<");
        btnVoltarCli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVoltarCliActionPerformed(evt);
            }
        });
        pnlControlesCli.add(btnVoltarCli);

        lblContador1Cli.setText("0");
        pnlControlesCli.add(lblContador1Cli);

        lblContador2.setText("de");
        pnlControlesCli.add(lblContador2);

        lblContador3.setText("( 0 )");
        pnlControlesCli.add(lblContador3);

        btnSeguirCli.setText(">");
        btnSeguirCli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSeguirCliActionPerformed(evt);
            }
        });
        pnlControlesCli.add(btnSeguirCli);

        btnUltimoCli.setText(">|");
        btnUltimoCli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUltimoCliActionPerformed(evt);
            }
        });
        pnlControlesCli.add(btnUltimoCli);

        lblCodCli.setText("CÓDIGO:");

        lblNomeCli.setText("NOME:");

        lblDataCadastro.setText("DATA DE CADASTRO:");

        lblTEL.setText("TEL.RESIDENCIAL:");

        lblCEL.setText("TEL.CELULAR:");

        txtNome.setEnabled(false);

        txtCod.setEditable(false);
        txtCod.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtCod.setText("1");
        txtCod.setOpaque(false);

        try {
            txtData.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("01-01-2012")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtData.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtData.setEnabled(false);

        try {
            txtTelefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtTelefone.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtTelefone.setEnabled(false);

        try {
            txtCelular.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtCelular.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCelular.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(pnlBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(22, 22, 22)
                        .addComponent(pnlControlesCli, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblCodCli)
                        .addGap(4, 4, 4)
                        .addComponent(txtCod, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(70, 70, 70)
                        .addComponent(lblDataCadastro)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtData, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblNomeCli)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNome, javax.swing.GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblTEL)
                        .addGap(4, 4, 4)
                        .addComponent(txtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(lblCEL)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCelular, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(pnlBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(pnlControlesCli, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(7, 7, 7)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(lblCodCli))
                    .addComponent(txtCod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(lblDataCadastro))
                    .addComponent(txtData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(lblNomeCli))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtCelular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(lblTEL))
                    .addComponent(txtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(lblCEL)))
                .addContainerGap())
        );

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nome", "Cadastrado em:", "Cadastrado por:", "Telefone", "Celular"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        try {
            ClienteDao dao = new ClienteDao();
            clientes = dao.pesquisar();
            //resize(true);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(FrmCadastroCliente.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(FrmCadastroCliente.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (!clientes.isEmpty()) {
            setData(0);
        }
    }//GEN-LAST:event_formWindowOpened

    private void btnVoltarInicioCliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVoltarInicioCliActionPerformed
        setData(0);
    }//GEN-LAST:event_btnVoltarInicioCliActionPerformed

    private void btnUltimoCliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUltimoCliActionPerformed
        setData(clientes.size() - 1);
    }//GEN-LAST:event_btnUltimoCliActionPerformed

    private void btnVoltarCliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVoltarCliActionPerformed
        setData(intPosition - 1);
    }//GEN-LAST:event_btnVoltarCliActionPerformed

    private void btnSeguirCliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSeguirCliActionPerformed
        setData(intPosition + 1);
    }//GEN-LAST:event_btnSeguirCliActionPerformed

    public void setData(int index) {
        intPosition = index;
        txtCod.setText(String.valueOf(clientes.get(index).getCodigo()));
        txtNome.setText(clientes.get(index).getNome());
        txtData.setText(clientes.get(index).getDataCadastro());
        txtTelefone.setText(clientes.get(index).getTelefone());
        txtCelular.setText(clientes.get(index).getCelular());
        if (intPosition == 0) {
            setEnabledNavigator(false);
        } else if (intPosition == clientes.size() - 1) {
            setEnabledNavigator(true);
        }
    }

    public void setEnabledNavigator(boolean b) {
        btnVoltarInicioCli.setEnabled(b);
        btnVoltarCli.setEnabled(b);
        btnSeguirCli.setEnabled(!b);
        btnUltimoCli.setEnabled(!b);
    }

    public void setActionListener(ActionListener al) {
        btnAdicionarCli.addActionListener(al);
        btnAdicionarCli.setActionCommand("Adicionar");
        btnDeletarCli.addActionListener(al);
        btnDeletarCli.setActionCommand("Deletar");
        btnAlterarCli.addActionListener(al);
        btnAlterarCli.setActionCommand("Alterar");
        btnSalvar.addActionListener(al);
        btnSalvar.setActionCommand("Salvar");
        btnCancelar.addActionListener(al);
        btnCancelar.setActionCommand("Cancelar");
    }
    
    public void setEnabledToolBar(boolean b) {
        btnAdicionarCli.setEnabled(b);
        btnAlterarCli.setEnabled(b);
        btnDeletarCli.setEnabled(b);
        btnSalvar.setEnabled(!b);
        btnCancelar.setEnabled(!b);
    }
    
    public void habilitaCampos(boolean b) {
        txtCelular.setEnabled(b);
        txtData.setEnabled(b);
        txtNome.setEnabled(b);
        txtTelefone.setEnabled(b);
    }
    
    public void resize(boolean b) {
        if(b) {
            setSize(jPanel1.getSize().width, jPanel1.getSize().height);
        } else {
            setSize(jPanel1.getSize().width + jPanel2.getSize().width + 10, 
                    jPanel1.getSize().height);
        }
    }
    
    public void limpaCampos(boolean b) {
        txtCelular.setText("");
        txtCod.setText(String.valueOf(clientes.size()));
        txtData.setText("");
        txtNome.setText("");
        txtTelefone.setText("");
    }
    
    public ClienteBean carregaBean(int codUser) {
        ClienteBean bean = new ClienteBean(txtNome.getText(), 
                txtCelular.getText(), txtData.getText(), txtTelefone.getText(),
                Integer.parseInt(txtCod.getText()), codUser);
        return bean;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new FrmCadastroCliente().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSeguirCli;
    private javax.swing.JButton btnUltimoCli;
    private javax.swing.JButton btnVoltarCli;
    private javax.swing.JButton btnVoltarInicioCli;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lblBotaoAdicionar;
    private javax.swing.JLabel lblBotaoAlterar;
    private javax.swing.JLabel lblBotaoCancelar;
    private javax.swing.JLabel lblBotaoDeletar;
    private javax.swing.JLabel lblBotaoSalvar;
    private javax.swing.JLabel lblCEL;
    private javax.swing.JLabel lblCodCli;
    private javax.swing.JLabel lblContador1Cli;
    private javax.swing.JLabel lblContador2;
    private javax.swing.JLabel lblContador3;
    private javax.swing.JLabel lblDataCadastro;
    private javax.swing.JLabel lblNomeCli;
    private javax.swing.JLabel lblTEL;
    private javax.swing.JPanel pnlBotoes;
    private javax.swing.JPanel pnlControlesCli;
    private javax.swing.JFormattedTextField txtCelular;
    private javax.swing.JTextField txtCod;
    private javax.swing.JFormattedTextField txtData;
    private javax.swing.JTextField txtNome;
    private javax.swing.JFormattedTextField txtTelefone;
    // End of variables declaration//GEN-END:variables
}
